package com.Mms.action;


import com.Mms.Vo.LoginVo;
import com.Mms.Vo.Theam;
import com.Mms.model.Major;
import com.Mms.model.Muser;
import com.Mms.service.serviceBySelect;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;

@Controller
@RequestMapping("/Login")
public class LoginAction {

    @Autowired
    serviceBySelect serviceBySelect;

    @RequestMapping(value = "/login",method = {RequestMethod.GET,RequestMethod.POST})
    public String Login(HttpServletResponse response){
        return "jsp/login";
    }

    @RequestMapping(value = "/add",method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView add(HttpServletResponse response, ModelAndView view){
        view.setViewName("jsp/add");
        return view;
    }
    @RequestMapping(value = "/send",method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView send(HttpServletResponse response, ModelAndView view){
        view.setViewName("jsp/send");
        return view;
    }
    @RequestMapping(value = "/chaxun",method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView chaxun(HttpServletResponse response, ModelAndView view){
        view.setViewName("jsp/chaxun");
        return view;
    }
    @RequestMapping(value ="/index",method = {RequestMethod.POST,RequestMethod.GET})
    public ModelAndView index(HttpServletResponse response, HttpServletRequest request, ModelAndView view, HttpSession session) {
        String ustuno=request.getParameter("ustuno");
        String upassword=request.getParameter("upassword");
        LoginVo loginVo = serviceBySelect.selectByLogin(ustuno,upassword);
         System.out.println(loginVo.getTheam().getAnymessages().size());
        Muser muser = loginVo.getMuser();
        Theam theam = loginVo.getTheam();
        for(int i =0;i<theam.getAnymessages().size();i++){
       System.out.println(theam.getAnymessages().get(i).getTheme());
       System.out.println(theam.getAnymessages().size());
        }
      //  String UserInfoJson = JSON.toJSONString(loginVo);
        view.setViewName("jsp/index");
        view.addObject("muser",muser);
        session.setAttribute("muser",muser);
        session.setAttribute("theam",theam);
        session.setAttribute("anmessage",loginVo.getAnymessage());
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return view;
    }
    /**
     * 登陆 用户输入用户名密码  密码错误或者用户名错误 提示用户名或密码错误，
     * 登录成功 进入首页 首页展示该成员的基本信息 基本信息 在json里有
     * 而且中间显示社联全部活动  在Login 类里有theam对象  这个对象就是显示在首页中间
     *anymessage 为本社团所有的通知和消息 在右边显示即可
     * @param ustuno  学号
     * @param upassword 密码
     * @param response
     */
    @RequestMapping(value = "/check",method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView Login(@RequestParam(value = "ustuno") String ustuno, @RequestParam(value = "upassword")String upassword, HttpServletResponse response){

        ModelAndView modelAndView = new  ModelAndView();
        modelAndView.setViewName("jsp/index");
        LoginVo loginVo = serviceBySelect.selectByLogin(ustuno,upassword);
        String UserInfoJson = JSON.toJSONString(loginVo);
        if ( loginVo.getFlag() == 1){
            modelAndView.addObject("loginVo",UserInfoJson);
        }else {
            modelAndView.setViewName("jsp/login");
            modelAndView.addObject("loginVo","用户名或密码错误");
        }
        return  modelAndView;
    }
}
